const app = getApp();

Page({
  data: {
    goods: [],
    cart: {},
    total: 0,
    addressList: [],
    addressItem: {
      id: '',
      userName: '',
      telNumber: '',
      detailInfo: '',
    },
    addressItemOld: {
      id: '',
      userName: '',
      telNumber: '',
      detailInfo: '',
    },
    shop: {
      giveMoney: 0,
      giveFree: 0,
      shopId: ''
    },
    enterType: 'init',
    backType:'back'
  },
  onLoad: function (options) {
    this.setData({
      enterType: options.enterType,
      cart: app.Store.get('cart'),
      goods: app.Store.get('confirm')
    })
    this.getAddress({ type: 'init' })

  },
  onShow: function () {
    let shopDetail = app.Store.get('shopDetail')
    if (shopDetail) {
      this.setData({
        shop: {
          giveMoney: Number(shopDetail.giveMoney),
          giveFree: Number(shopDetail.giveFree),
          shopId: Number(shopDetail.shopId)
        }
      })
    }
   
    
  },
  getAddress(objData) {
   
    app.Http.http({
      url: app.Api.AddressList,
      method: 'POST',
      params: {}
    }).then(data => {
      let result = data

      if (result.length > 0) {
        if (objData.type === 'init') {
          this.setData({
            addressList: result,
            addressItem: {
              id: result[0].id,
              userName: result[0].userName,
              telNumber: result[0].telNumber,
              detailInfo: result[0].detailInfo
            }
          })
        } else {
          result.map((item) => {
            this.setData({
              addressList: result
            })
            if (item.id == objData.data.addressId) {
              console.log(objData.data.addressId)
              this.setData({
                addressItem: {
                  id: item.id,
                  userName: item.userName,
                  telNumber: item.telNumber,
                  detailInfo: item.detailInfo
                }
              })
            }
          })
        }

      } else {
        this.setData({
          addressList: [],
          addressItem: {
            id: '',
            userName: '',
            telNumber: '',
            detailInfo: ''
          }
        })
      }

    }).catch(err => {
      console.log(err)
    })
  },
  toAddress() {
    this.setData({
      backType: 'back'
    })
    app.WxService.navigateTo(`/pages/address/index`, { enter: 'order' })
  },
  onPullDownRefresh: function () {

  },
  submit() {//OrderCreate
    let obj = {}
    obj.addressId = Number(this.data.addressItem.id)
    obj.shopId = Number(this.data.shop.shopId)
    obj.goodList = []
    Object.keys(this.data.cart.list).forEach((key) => {
      obj.goodList.push({
        goodId: Number(key),
        goodNum: this.data.cart.list[key]
      })
    })
    let url = this.data.enterType === 'user' ? app.Api.OrderShopCreate : app.Api.OrderCreate
    if (obj.addressId){
      app.Http.http({
        url: url,
        method: 'POST',
        params: obj
      }).then(data => {
        app.WxService.navigateTo('/pages/pay/index', { orderNum: data, money: this.data.cart.total >= this.data.shop.giveFree ? this.data.cart.total : (this.data.cart.total + this.data.shop.giveMoney) })
      }).catch(err => {
        console.log(err)
      })
    }else{
      wx.showToast({
        title: '请添加地址',
        icon: 'success',
        duration: 2000
      })
    }
   
  },
  changeData(data) {
    if (JSON.stringify(data) !== "{}") {
      this.setData({
        backType:'select'
      })
      this.getAddress({ type: 'back', data })
    } else {
      this.getAddress({ type: 'init' })
    }
  }
})